﻿using System.Drawing;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace WinFormsPrintDemo
{
    class QrCodeUtil
    {
        public static Bitmap QrCode(string msg, int size, int margin)
        {
            BarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.QR_CODE
            };
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
            writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
            writer.Options.Height = writer.Options.Width = size;
            writer.Options.Margin = margin;//设置边框
            BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            return img;
        }

        public static Bitmap Code128(string msg, int width, int height)
        {
            EncodingOptions options = new EncodingOptions
            {
                Margin = 0,
                Width = width,
                Height = height
            };
            BarcodeWriter writer = new BarcodeWriter
            {
                Format = BarcodeFormat.CODE_128,
                Options = options
            };
            BitMatrix bm = writer.Encode(msg);
            return writer.Write(bm);
        }
    }
}
